<?php
/*
 * Главная страница
 */
class Page extends BasicModel {
	public function __construct(Core $Site) {
		parent::__construct($Site);
	}
	public function makePage() {
		$NowDate = $Weather = $City = $District = $Area = NULL;
		$tDate = date('Y-m-d');
		// настройка параметров кэширования
		$this->Config->Cache['city.page']['CACHE_NAME']	.= $this->URL->Page['ID'];
		$this->MySQL->CacheSetConfig($this->Config->Cache['city.page']);
		// загрузка кэша
		$this->MySQL->CacheLoad();
		if ($this->MySQL->CacheIsSet('NOW_DATE')) {$NowDate = $this->MySQL->CacheGet('NOW_DATE', TRUE);}
		// запись кэша по номой при новом дне
		if ($NowDate != $tDate) {
			$this->MySQL->CacheClear();
			$NowDate = $tDate;
			$this->MySQL->CacheWrite($NowDate, 'NOW_DATE');
		}
		// город
		if (!$this->MySQL->CacheIsSet('CITY')) {
			$City = $this->MySQL->Select('SELECT `c`.`ID`, `DISTRICT_ID`, `AREA_ID`, `NAME`, `PAGE_H1`, `PAGE_TEXT`, `PAGE_IMAGE` FROM `{p}cities` `c` INNER JOIN `{p}description` `d` ON `d`.`CITY_ID` = `c`.`ID` WHERE `c`.`ID` = '.$this->URL->Page['ID']);
			if ($City) {$this->MySQL->CacheWrite($City, 'CITY');}
			else {$this->Site->go404();}
		} else {$City = $this->MySQL->CacheGet('CITY', TRUE);}
		// погода
		if (!$this->MySQL->CacheIsSet('WEATHER')) {
			$Weather = $this->MySQL->Select('SELECT * FROM `{p}weathers` WHERE `CITY_ID` = '.$City['ID'].' AND `DATE` >= "'.$NowDate.'"', MySQL::SAll);
			if ($Weather) {$this->MySQL->CacheWrite($Weather, 'WEATHER');}
		} else {$Weather = $this->MySQL->CacheGet('WEATHER', TRUE);}
		// район
		if (!$this->MySQL->CacheIsSet('DISTRICT')) {
			$District = $this->MySQL->Select('SELECT `d`.`ID`, `NAME`, `URL` FROM `{p}districts` `d` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `d`.`ID` WHERE `d`.`ID` = '.$City['DISTRICT_ID'].' AND `URL_TYPE` = 2');
			if ($District) {$this->MySQL->CacheWrite($District, 'DISTRICT');}
		} else {$District = $this->MySQL->CacheGet('DISTRICT', TRUE);}
		// область
		if (!$this->MySQL->CacheIsSet('AREA')) {
			$Area = $this->MySQL->Select('SELECT `a`.`ID`, `NAME`, `URL` FROM `{p}areas` `a` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `a`.`ID` WHERE `a`.`ID` = '.$City['AREA_ID'].' AND `URL_TYPE` = 1');
			if ($Area) {$this->MySQL->CacheWrite($Area, 'AREA');}
		} else {$Area = $this->MySQL->CacheGet('AREA', TRUE);}
		// сохранение кэша
		//$this->MySQL->CacheSave();
		// настройка страницы
		$this->Template->setTitle($City['NAME']); // тайтл
		$this->Template->setLoadPage('city.page'); // шаблон
		$this->Template->setPageElements(array ( // элементы страницы
			'CITY'		=> $City,
			'WEATHER'	=> $Weather
		));
		$this->Template->addCrumbs(array (
			'/'	=> $this->Language->LText['MAIN'],
			$Area['URL']	=> $Area['NAME'],
			$District['URL'] => $District['NAME'],
			'' => $City['NAME']
		));
		return 0;
	}
}
?>